//
//  ViewController.m
//  YLDragZoomCycleViewDemo
//
//  Created by 张雨露 on 2017/5/26.
//  Copyright © 2017年 张雨露. All rights reserved.
//

#define kCurrentSystemVersion ([[[UIDevice currentDevice] systemVersion] floatValue])

#import "ViewController.h"
#import "YLSecondViewController.h"
#import "XMGMeCell.h"
#import "XMGMeFooterView.h"
#import "loveTableViewCell.h"

#import "XMGWebVc.h"

@interface ViewController ()<loveTableViewCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;


@property (nonatomic,copy) NSArray *customerArray;

@property (nonatomic,copy) NSArray *loveArray;


@property (nonatomic,copy) NSArray *productArray;
/**代里帮助*/
@property (nonatomic,copy) NSArray *delegateHelptArray;

@property (nonatomic,weak)  loveTableViewCell *lovecell;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
}

- (void)setupTableView
{
    // 设置背景色
    self.tableView.backgroundColor = XMGGlobalBg;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[XMGMeCell class] forCellReuseIdentifier:@"XMGMeId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"loveTableViewCell" bundle:nil] forCellReuseIdentifier:@"loveTableViewCell"];
    
    
    self.tableView.sectionFooterHeight =1;
    self.tableView.rowHeight=UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight=100;
    // 调整header和footer
    if (kCurrentSystemVersion >= 11.0) {
        _tableView.estimatedRowHeight = 0;
        _tableView.sectionHeaderHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.sectionFooterHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
    }
    
    // 调整inset
    // 设置footerView
    self.tableView.contentInset = UIEdgeInsetsMake(1, 0, 0, 0);
//    self.tableView.tableHeaderView = [[XMGMeFooterView alloc] init];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden: YES animated: NO];
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController setNavigationBarHidden: YES animated: NO];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    CGFloat catoryH=0;
    if (indexPath.section==0) {
        NSLog(@" product: %lu",(unsigned long)_loveArray.count);
        
        CGFloat y = (self.loveArray.count +4-1 )/4;
        
        if (self.loveArray.count>4) {
            
            catoryH=20;
        }else  {
            
             catoryH=40;
        }
       return ([UIScreen mainScreen].bounds.size.width/4+catoryH) *y;
        
        
    }else if (indexPath.section==1) {
        
        if (self.customerArray.count>4) {
            catoryH=20;
        }else  {
            catoryH=45;
        }
        
        CGFloat y = (self.customerArray.count +4-1 )/4;
        CGFloat cellH=([UIScreen mainScreen].bounds.size.width/4+catoryH) *y;
        return cellH;
    }else if (indexPath.section==2) {
        
        if (self.productArray.count>4) {
            catoryH=20;
        }else  {
            catoryH=45;
        }
        
        CGFloat y = (self.productArray.count +4-1 )/4;
        CGFloat cellH=([UIScreen mainScreen].bounds.size.width/4+catoryH) *y;
        return cellH;
    } else{
        
        CGFloat y = (self.delegateHelptArray.count +4-1 )/4;
        
        if (self.delegateHelptArray.count>4) {
            catoryH=20;
        }else {
            catoryH=40;
        }
        
        return ([UIScreen mainScreen].bounds.size.width/4+catoryH) *y;
        
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        loveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"loveTableViewCell"];
        cell.array = self.loveArray;
        cell.loveDelegate=self;
        
        cell.titleLable.text=@"行业领先，帮你获得更多的客户资源";
        return cell;
    }else if (indexPath.section == 1) {
        
        loveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"loveTableViewCell"];
        cell.array = self.customerArray;
        cell.loveDelegate=self;
        
        cell.titleLable.text=@"帮你加群和吸粉，支持主动添加和被动添加";
        
        return cell;
    } else if (indexPath.section == 2) {
        
        loveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"loveTableViewCell"];
        cell.array = self.productArray;
        cell.loveDelegate=self;
        
        cell.titleLable.text=@"让你的产品覆盖全网，占领搜索首页";
        
        return cell;
    }else  {
        
        loveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"loveTableViewCell"];
        cell.array = self.delegateHelptArray;
        cell.loveDelegate=self;
        
        cell.titleLable.text=@"代理帮助";
        
        return cell;
    }
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view=[[UIView alloc]init];
    
    return view;
    
}


#pragma mark == loveTableViewCellDelegate

-(void)loveTableViewCellDelegate:(XMGSqaureButton *)squareButton{
    
    XMGLog( @"=== %@",squareButton.square.name);
    
    
    XMGWebVc *web=[[XMGWebVc alloc]init];
    web.url=squareButton.square.url;
    
    NSString *title=squareButton.square.name;
    
    web.title_Lable=title;
    
    [self.navigationController pushViewController:web animated:YES];
    
    XMGLogFunc;
}
#pragma mark ===  行业领先，帮你获得更多的客户资源
- (NSArray *)loveArray{
    
    if (!_loveArray) {
        NSArray *square_list=@[
                               @{
                                   @"icon":@"guoji",
                                   @"name":@"中国客源",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"guonei",
                                   @"name":@"国际客源",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"jingzhukeyuan",
                                   @"name":@"精准客源",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"zidaokeyuan",
                                   @"name":@"自导客源",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"lianghaokeyuan",
                                   @"name":@"靓号客源",
                                   @"url":@"小明"
                                   }
                               ];
        
        NSArray *sqaures = [XMGSquare objectArrayWithKeyValuesArray:square_list];
        _loveArray=sqaures;
        
    }
    return _loveArray;
}
#pragma mark ===帮你加群和吸粉，支持主动添加和被动添加
- (NSArray *)customerArray{
    
    if (!_customerArray) {
        NSArray *square_list=@[
                               @{
                                   @"icon":@"weixinaadd",
                                   @"name":@"微信加群",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"weixinhudong",
                                   @"name":@"微信互粉",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"qqjia",
                                   @"name":@"QQ加群",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"qqhufen",
                                   @"name":@"QQ互粉",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"tijiaobaofen",
                                   @"name":@"提交爆粉",
                                   @"url":@"小明"
                                   }
                               ];
        
        NSArray *sqaures = [XMGSquare objectArrayWithKeyValuesArray:square_list];
        _customerArray=sqaures;
        
    }
    return _customerArray;
}

#pragma mark ===帮你加群和吸粉，支持主动添加和被动添加

- (NSArray *)productArray{
    
    if (!_productArray) {
        
        NSArray *square_list=@[
                               @{
                                   @"icon":@"xiezi",
                                   @"name":@"图文推广",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"diannao",
                                   @"name":@"视频推广",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"xuanzhuan",
                                   @"name":@"提交推广",
                                   @"url":@"小明"
                                   }
                               ];
        
        NSArray *sqaures = [XMGSquare objectArrayWithKeyValuesArray:square_list];
        _productArray=sqaures;
    }
  return _productArray;
    
}
        
#pragma mark ===代理帮助

- (NSArray *)delegateHelptArray{
    
    if (!_delegateHelptArray) {
        
        NSArray *square_list=@[
                               @{
                                   @"icon":@"fangdaj",
                                   @"name":@"代理专区",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"sanheng",
                                   @"name":@"售后服务",
                                   @"url":@"小明"
                                   },
                               @{
                                   @"icon":@"meihua",
                                   @"name":@"关于软件",
                                   @"url":@"小明"
                                   }
                               ];
        
        NSArray *sqaures = [XMGSquare objectArrayWithKeyValuesArray:square_list];
        _delegateHelptArray=sqaures;
    }
    return _delegateHelptArray;
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

//    Xm_WeChatVc *vc = [[Xm_WeChatVc alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    
    YLSecondViewController *vc = [[YLSecondViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
    
}

@end
